package org.example;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.NodeOrientation;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;

import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.FlowPane;

import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;


import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * AnchorPane:绝对与相对布局
 * */
public class Main extends Application {
    public static double xOffset = 0;
    public static double yOffset = 0;
    public static double sa = 0;
    public FlowPane flowPane = new FlowPane();
    private Button[] button = new Button[20];
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    double sfw = (dimension.getWidth()*0.3);
    double sfh = (dimension.getHeight()*0.5);
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(final Stage primaryStage) {
        flowPane.setAlignment(Pos.TOP_CENTER);
        flowPane.setOrientation(Orientation.HORIZONTAL);
        flowPane.setPadding(new Insets(20,20,20,20));
        flowPane.setPrefSize(sfw,sfh);
        flowPane.setVgap(20);
        flowPane.setHgap(20);
        int s = 0;
        for (Button b:button) {
            b = new Button((s++)+"");
            b.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
            b.setMinSize(Double.MIN_VALUE,Double.MIN_VALUE);
            b.setPrefSize(flowPane.getPrefWidth()*0.3,100);
            flowPane.setStyle("-fx-background-color: aqua;-fx-font-size: "+25d+"px;");
            flowPane.getChildren().add(b);
        }

        Scene scene = new Scene(flowPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(sfw);
        primaryStage.setHeight(sfh);
        primaryStage.setTitle("Login");
//        primaryStage.resizableProperty().setValue(Boolean.FALSE);
//        primaryStage.setResizable(false);
        primaryStage.initStyle(StageStyle.values()[0]);
        primaryStage.setAlwaysOnTop(true);



        flowPane.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        flowPane.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setX(event.getScreenX() - xOffset);
                primaryStage.setY(event.getScreenY() - yOffset);
            }
        });
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                System.exit(0);
            }
        });
        ChangeListener<Number> listener = new ChangeListener<Number>() {
            final Timer timer = new Timer(); // uses a timer to call your resize method
            TimerTask task = null; // task to execute after defined delay
            final long delayTime = 50; // delay that has to pass in order to consider an operation done
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, final Number newValue)
            {
                if (task != null)
                { // there was already a task scheduled from the previous operation ...
                    task.cancel(); // cancel it, we have a new size to consider
                }
                task = new TimerTask() // create new task that calls your resize operation
                {
                    @Override
                    public void run()
                    {
                        double dpr = sfw/sfh;
                        flowPane.setStyle("-fx-background-color: aqua;-fx-font-size: "+(25d/dpr)+"px");
                        // here you can place your resize code
                        List list =Arrays.asList(flowPane.getChildren().toArray());
                        for (Object aa:list) {

                            ((Button)aa).setPrefSize(primaryStage.getWidth()*0.2,primaryStage.getHeight()*0.2);
                        }
                    }
                };
                timer.schedule(task, delayTime);
            }
        };
        primaryStage.widthProperty().addListener(listener);
        primaryStage.heightProperty().addListener(listener);
        primaryStage.show();
    }
}
